package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shisfish.util.SqlUtils;
import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.dao.IFeedBackDao;
import com.zhijiaoyun.model.FeedBack;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.IFeedBackServie;

/**
 * @author shisfish
 * @date 2018/1/18
 */
@Service(value = "feedBackService")
public class FeedBackServiceImpl implements IFeedBackServie {

    @Autowired
    IFeedBackDao feedBackDao;

    @Override
    public PageList<FeedBack> page(int pageIndex, int pageSize, String content) {
        if (StringUtils.isNotEmptyString(content)) {
            content = SqlUtils.setPercentAll(content);
        }
        int count = feedBackDao.count(content);
        List<FeedBack> list = new ArrayList<>();
        PageData page = new PageData();
        if (count > 0) {
            page = PageData.createPage(count, pageIndex, pageSize);
            list = feedBackDao.page(page.getBeginIndex(), page.getPageSize(), content);
        }
        PageList<FeedBack> pageList = new PageList<>(list, page);
        return pageList;
    }

    @Override
    public void addFeedBack(FeedBack feedBack) {
        feedBackDao.insert(feedBack);
    }

    @Override
    public void saveFeedBack(String content, String device, HttpServletRequest request) {
        long userId = StringUtils.parseLong(String.valueOf(request.getAttribute("userId")), 0L);
        FeedBack feedBack = new FeedBack();
        feedBack.setUserId(userId);
        feedBack.setContext(content);
        feedBack.setDevice(device);
        addFeedBack(feedBack);
    }
}
